﻿/* 
 *  <copyright file="BaseUser.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using SobrietyEngine.Guids;
using SobrietyEngine.Input;
using SobrietyEngine.Input.Mapping;
using SobrietyEngine.Logging;
using SobrietyEngine.PlayerData;

namespace SobrietyEngine.User
{
    public class BaseUser: IUser
    {
        public String UserName {get; private set; }

        public CGuid Guid { get; set; }
        public IPlayerGameData GameData { get; set; }
        public List<IInputMapper> InputMappers { get; set; }
        public Boolean UsingGamepad { get; set; }
        public int GamepadIndex { get; set; }
        public Boolean UsingKeyboard { get; set; }

        public Boolean Local { get; private set; }
        public Boolean Active { get; set; }
        public List<IInputDevice> InputDevices { get; set; }


        public BaseUser(String userName)
        {
            UserName = userName;
            InputMappers = new List<IInputMapper>();
            InputDevices = new List<IInputDevice>();
            Guid = CGuid.Create(UserName, this);
        }

        public void AddInputMapper(IInputMapper mapper)
        {
            if (InputMappers.Contains(mapper))
                return;
            InputMappers.Add(mapper);
        }

        private IInputMapper GetMapperByType(String type)
        {
            foreach (IInputMapper i in InputMappers)
            {
                if (type == i.GetType().ToString())
                    return i;
            }
            Logger.GetSingleton().WriteError("User::GetMapperByType()::mapper not found: " + type);
            return null;
        }

        public void RemoveInputMapper(Type typeOfMapper)
        {
            RemoveInputMapper(typeOfMapper.ToString());
        }

        public void DisableInputMapper(Type typeOfMapper)
        {
            DisableInputMapper(typeOfMapper.ToString());
        }

        public void EnableInputMapper(Type typeOfMapper)
        {
            EnableInputMapper(typeOfMapper.ToString());
        }

        public void RemoveInputMapper(String typeOfMapper)
        {
            IInputMapper i = GetMapperByType(typeOfMapper);            
            i.UnregisterEventListeners();
            InputMappers.Remove(i);
        }
        public void DisableInputMapper(String typeOfMapper)
        {
            IInputMapper i = GetMapperByType(typeOfMapper);
            if(i.Enabled)
                i.UnregisterEventListeners();
        }

        public void EnableInputMapper(String typeOfMapper)
        {
            IInputMapper i = GetMapperByType(typeOfMapper);
            //i.UnregisterEventListeners();
            if (!i.Enabled)
                i.RegisterEventListeners();                       
        }

        public void DisableAllInputMappers()
        {
            foreach (IInputMapper i in InputMappers)
                if(i.Enabled)
                    i.UnregisterEventListeners();
        }

        public Boolean HasInputMapper(Type typeOfMapper)
        {
            foreach (IInputMapper i in InputMappers)
            {
                if (typeOfMapper == i.GetType())
                    return true;
            }
            return false;
        }
    }
}
